limsArchivesManage.detailPage = {

    getBtnToolbar : function(){
        let _this = this;
        return {
            type : 'btnToolbar',
            items : [{
                type : 'button',
                text : 'GIKAM.BUTTON.BACK',
                icon : 'back',
                onClick : function() {
                    workspace.window.goBack();
                }
            }]
        }
    },

    getBaseInfoTab : function () {
        let _this = this
        return {
            type: 'tab',
            panels: [{
                title: 'BASEMODULE.RM.ARCHIVESMANAGES.TITLE.BORROW_RECORD',
                items: [{
                    type: 'layout',
                    north: {
                        height: '50%',
                        items: [_this.getArchivesBorrowGrid()]
                    },
                    center: {
                        items: [_this.getAttachmantStruct(_this.idManager.archivesBorrowGrid, 'T_LIMS_ARCHIVES_HANDLE')]
                    }
                }]
            }, {
                title: 'BASEMODULE.RM.ARCHIVESMANAGES.TITLE.MOVE_RECORD',
                items: [{
                    type: 'layout',
                    north: {
                        height: '50%',
                        items: [_this.getArchivesMoveGrid()]
                    },
                    center: {
                        items: [_this.getAttachmantStruct(_this.idManager.archivesMoveGrid, 'T_LIMS_ARCHIVES_HANDLE')]
                    }
                }]
            }, {
                title: 'BASEMODULE.RM.ARCHIVESMANAGES.TITLE.DESTORY_RECORD',
                items: [{
                    type: 'layout',
                    north: {
                        height: '50%',
                        items: [_this.getArchivesDestoryGrid()]
                    },
                    center: {
                        items: [_this.getAttachmantStruct(_this.idManager.archivesDestoryGrid, 'T_LIMS_ARCHIVES_HANDLE')]
                    }
                }]
            }, {
                title: 'GIKAM.FILE.ATTACHMENT',
                items: [_this.getAttachmantStruct2(_this.idManager.archivesGrid, 'T_LIMS_ARCHIVES_MANAGE')]
            }]
        }
    },

    getAttachUploaderId: function (gridId) {
        return gridId + '-uploader'
    },

    refreshAttachments: function (gridId) {
        let _this = this
        let grid = Gikam.getComp(gridId);
        let actRow = grid.getActivedRow();
        Gikam.getAsyncComp(_this.getAttachUploaderId(gridId)).done(uploader => {
            if (actRow && actRow.id) {
                uploader.setOptions({
                    bizId: actRow.id,
                })
            } else {
                uploader.setOptions({
                    bizId: -1,
                })
            }
        })
    },

    getArchivesBorrowGrid: function () {
        var _this = this;
        return {
            type: 'grid',
            id: _this.idManager.archivesBorrowGrid,
            url: _this.urlManager.archivesHandleGrid + '/queries/raw',
            requestData: {
                archivesCode_EQ: _this.param.archivesCode,
                handleType_EQ: 'borrow'
            },
            service: 'limsArchivesHandleServiceImpl',
            dbTable: 'T_LIMS_ARCHIVES_HANDLE',
            columns: limsArchivesManage.getBorrowEditListColumns(),
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0)
                } else {
                    _this.refreshAttachments(_this.idManager.archivesBorrowGrid)
                }
            },
            onCellClick: function (field, row) {
                field === 'archivesCode' && _this.getArchivesBorrowEditModal(row)
            },
            onRowActive: function (index, row) {
                _this.refreshAttachments(_this.idManager.archivesBorrowGrid)
            },
            deleteFormatter: function (row) {
                return row.archivesCode
            },
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                onClick: function () {
                    let archivesGrid = Gikam.getComp(_this.idManager.archivesGrid)
                    let actRow = archivesGrid.getActivedRow()
                    if (Gikam.isEmpty(actRow)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM')
                        return
                    }
                    if (!actRow.archivesCode || actRow.archivesCode === '') {
                        Gikam.alert('BASEMODULE.RM.ARCHIVESMANAGES.TIP.ARCHIVESCODE_IS_NULL')
                        return
                    }
                    if (actRow.status === 'destoryed') {
                        Gikam.alert('BASEMODULE.RM.ARCHIVESMANAGES.TIP.ARCHIVESCODE_IS_DESTORYED')
                        return
                    }
                    Gikam.preInsert({
                        modalTitle: 'BASEMODULE.RM.ARCHIVESMANAGES.MODAL.ADD',
                        fields: limsArchivesManage.getBorrowBaseInfoFormFields(),
                        url: _this.urlManager.archivesHandleGrid,
                        disableNotEmptyValidate: true,
                        param: {
                            archivesCode: actRow.archivesCode,
                            handleType: 'borrow'
                        }
                    }).done(function (id) {
                        Gikam.getComp(_this.idManager.archivesBorrowGrid).refresh()
                    })
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                onClick: function () {
                    Gikam.getComp(_this.idManager.archivesBorrowGrid).deleteRows(_this.urlManager.archivesHandleGrid)
                }
            }]
        }
    },

    getArchivesBorrowEditModal: function (row) {
        var _this = this
        var model = Gikam.create('modal', {
            title: 'GIKAM.BUTTON.EDIT',
            height: '520',
            width: '400',
        })
        Gikam.create('layout', {
            renderTo: model.window.$dom,
            center: {
                items: [{
                    type: 'form',
                    id: _this.idManager.archivesBorrowGrid + '-edit-form',
                    titleWidth: 80,
                    columns: 1,
                    fields: limsArchivesManage.getBorrowBaseInfoFormFields(),
                    onLoadSuccess: function () {
                    },
                    url: Gikam.printf(_this.urlManager.archivesHandleGrid + '/{id}', {
                        id: row.id
                    }),
                    onRendered: function () {
                    }
                }, {
                    type: 'btnToolbar',
                    items: [{
                        type: 'button',
                        text: 'GIKAM.BUTTON.CONFIRM',
                        class: 'blue',
                        icon: 'save',
                        color: 'white',
                        onClick: function () {
                            var form = Gikam.getComp(_this.idManager.archivesBorrowGrid + '-edit-form')
                            if (form.validate()) {
                                var data = form.getData()
                                var url = Gikam.printf(_this.urlManager.archivesHandleGrid + '/{id}', {
                                    id: row.id
                                })
                                Gikam.put(url, Gikam.getJsonWrapper(null, [null, [data]])).done(function () {
                                    Gikam.getComp(_this.idManager.archivesBorrowGrid).refresh()
                                })
                                Gikam.getLastModal().close(data)
                            }
                        }
                    }, {
                        type: 'button',
                        text: 'GIKAM.BUTTON.CANCEL',
                        icon: 'back',
                        onClick: function () {
                            Gikam.getLastModal().close()
                        }
                    }]
                }]
            }
        })
    },

    getArchivesMoveGrid: function () {
        var _this = this
        return {
            type: 'grid',
            id: _this.idManager.archivesMoveGrid,
            url: _this.urlManager.archivesHandleGrid + '/queries/raw',
            requestData: {
                handleType_EQ: 'move',
                archivesCode_EQ : _this.param.archivesCode
            },
            service: 'limsArchivesHandleServiceImpl',
            dbTable: 'T_LIMS_ARCHIVES_HANDLE',
            columns: limsArchivesManage.getMoveEditListColumns(),
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0)
                } else {
                    _this.refreshAttachments(_this.idManager.archivesMoveGrid)
                }
            },
            onCellClick: function (field, row) {
                field === 'archivesCode' && _this.getArchivesMoveEditModal(row)
            },
            onRowActive: function (index, row) {
                _this.refreshAttachments(_this.idManager.archivesMoveGrid)
            },
            deleteFormatter: function (row) {
                return row.archivesCode
            },
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                onClick: function () {
                    let archivesGrid = Gikam.getComp(_this.idManager.archivesGrid)
                    let actRow = archivesGrid.getActivedRow()
                    if (Gikam.isEmpty(actRow)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM')
                        return
                    }
                    if (!actRow.archivesCode || actRow.archivesCode === '') {
                        Gikam.alert('BASEMODULE.RM.ARCHIVESMANAGES.TIP.ARCHIVESCODE_IS_NULL')
                        return
                    }
                    if (actRow.status === 'destoryed') {
                        Gikam.alert('BASEMODULE.RM.ARCHIVESMANAGES.TIP.ARCHIVESCODE_IS_DESTORYED')
                        return
                    }
                    Gikam.preInsert({
                        modalTitle: 'BASEMODULE.RM.ARCHIVESMANAGES.MODAL.ADD',
                        fields: limsArchivesManage.getMoveBaseInfoFormFields(),
                        url: _this.urlManager.archivesHandleGrid,
                        disableNotEmptyValidate: true,
                        param: {
                            archivesCode: actRow.archivesCode,
                            handleType: 'move'
                        }
                    }).done(function (id) {
                        Gikam.getComp(_this.idManager.archivesMoveGrid).refresh()
                    })
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                onClick: function () {
                    Gikam.getComp(_this.idManager.archivesMoveGrid).deleteRows(_this.urlManager.archivesHandleGrid)
                }
            }]
        }
    },

    getArchivesMoveEditModal: function (row) {
        var _this = this
        var model = Gikam.create('modal', {
            title: 'GIKAM.BUTTON.EDIT',
            height: '480',
            width: '400',
        })
        Gikam.create('layout', {
            renderTo: model.window.$dom,
            center: {
                items: [{
                    type: 'form',
                    id: _this.idManager.archivesMoveGrid + '-edit-form',
                    titleWidth: 80,
                    columns: 1,
                    fields: limsArchivesManage.getMoveBaseInfoFormFields(),
                    onLoadSuccess: function () {
                    },
                    url: Gikam.printf(_this.urlManager.archivesHandleGrid + '/{id}', {
                        id: row.id
                    }),
                    onRendered: function () {
                    }
                }, {
                    type: 'btnToolbar',
                    items: [{
                        type: 'button',
                        text: 'GIKAM.BUTTON.CONFIRM',
                        class: 'blue',
                        icon: 'save',
                        color: 'white',
                        onClick: function () {
                            var form = Gikam.getComp(_this.idManager.archivesMoveGrid + '-edit-form')
                            if (form.validate()) {
                                var data = form.getData()
                                var url = Gikam.printf(_this.urlManager.archivesHandleGrid + '/{id}', {
                                    id: row.id
                                })
                                Gikam.put(url, Gikam.getJsonWrapper(null, [null, [data]])).done(function () {
                                    Gikam.getComp(_this.idManager.archivesMoveGrid).refresh()
                                })
                                Gikam.getLastModal().close(data)
                            }
                        }
                    }, {
                        type: 'button',
                        text: 'GIKAM.BUTTON.CANCEL',
                        icon: 'back',
                        onClick: function () {
                            Gikam.getLastModal().close()
                        }
                    }]
                }]
            }
        })
    },

    getArchivesDestoryGrid: function () {
        var _this = this
        return {
            type: 'grid',
            id: _this.idManager.archivesDestoryGrid,
            url: _this.urlManager.archivesHandleGrid + '/queries/raw',
            requestData: {
                handleType_EQ: 'destory',
                archivesCode_EQ : _this.param.archivesCode
            },
            service: 'limsArchivesHandleServiceImpl',
            dbTable: 'T_LIMS_ARCHIVES_HANDLE',
            columns: limsArchivesManage.getDestoryEditListColumns(),
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0)
                } else {
                    _this.refreshAttachments(_this.idManager.archivesDestoryGrid)
                }
            },
            onCellClick: function (field, row) {
                field === 'archivesCode' && _this.getArchivesDestoryEditModal(row)
            },
            onRowActive: function (index, row) {
                _this.refreshAttachments(_this.idManager.archivesDestoryGrid)
            },
            deleteFormatter: function (row) {
                return row.archivesCode
            },
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                onClick: function () {
                    let archivesGrid = Gikam.getComp(_this.idManager.archivesGrid)
                    let actRow = archivesGrid.getActivedRow()
                    if (Gikam.isEmpty(actRow)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM')
                        return
                    }
                    if (!actRow.archivesCode || actRow.archivesCode === '') {
                        Gikam.alert('BASEMODULE.RM.ARCHIVESMANAGES.TIP.ARCHIVESCODE_IS_NULL')
                        return
                    }
                    if (actRow.status === 'destoryed') {
                        Gikam.alert('BASEMODULE.RM.ARCHIVESMANAGES.TIP.ARCHIVESCODE_IS_DESTORYED')
                        return
                    }
                    Gikam.preInsert({
                        modalTitle: 'BASEMODULE.RM.ARCHIVESMANAGES.MODAL.ADD',
                        fields: limsArchivesManage.getDestoryBaseInfoFormFields(),
                        url: _this.urlManager.archivesHandleGrid,
                        disableNotEmptyValidate: true,
                        param: {
                            archivesCode: actRow.archivesCode,
                            handleType: 'destory'
                        },
                        titleWidth: 90
                    }).done(function (id) {
                        Gikam.getComp(_this.idManager.archivesDestoryGrid).refresh()
                        archivesGrid.refreshRowById(actRow.id)
                    })
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                onClick: function () {
                    Gikam.getComp(_this.idManager.archivesDestoryGrid).deleteRows(_this.urlManager.archivesHandleGrid).done(function () {
                        var archGrid = Gikam.getComp(_this.idManager.archivesGrid)
                        archGrid.refreshRowById(archGrid.getActivedRow().id)
                    })
                }
            }]
        }
    },

    getArchivesDestoryEditModal: function (row) {
        var _this = this
        var model = Gikam.create('modal', {
            title: 'GIKAM.BUTTON.EDIT',
            height: '480',
            width: '400',
        })
        Gikam.create('layout', {
            renderTo: model.window.$dom,
            center: {
                items: [{
                    type: 'form',
                    id: _this.idManager.archivesDestoryGrid + '-edit-form',
                    titleWidth: 90,
                    columns: 1,
                    fields: limsArchivesManage.getDestoryBaseInfoFormFields(),
                    onLoadSuccess: function () {
                    },
                    url: Gikam.printf(_this.urlManager.archivesHandleGrid + '/{id}', {
                        id: row.id
                    }),
                    onRendered: function () {
                    }
                }, {
                    type: 'btnToolbar',
                    items: [{
                        type: 'button',
                        text: 'GIKAM.BUTTON.CONFIRM',
                        class: 'blue',
                        icon: 'save',
                        color: 'white',
                        onClick: function () {
                            var form = Gikam.getComp(_this.idManager.archivesDestoryGrid + '-edit-form')
                            if (form.validate()) {
                                var data = form.getData()
                                var url = Gikam.printf(_this.urlManager.archivesHandleGrid + '/{id}', {
                                    id: row.id
                                })
                                Gikam.put(url, Gikam.getJsonWrapper(null, [null, [data]])).done(function () {
                                    Gikam.getComp(_this.idManager.archivesDestoryGrid).refresh()
                                })
                                Gikam.getLastModal().close(data)
                            }
                        }
                    }, {
                        type: 'button',
                        text: 'GIKAM.BUTTON.CANCEL',
                        icon: 'back',
                        onClick: function () {
                            Gikam.getLastModal().close()
                        }
                    }]
                }]
            }
        })
    },

    getAttachmantStruct: function (gridId, dbTable) {
        let _this = this
        return {
            type: 'uploader',
            immediateUpload: true,
            id: _this.getAttachUploaderId(gridId),
            dbTable: dbTable,
            filter: false,
            genericQuery: false,
            onBeforeUpload: function () {
                var activedRow = Gikam.getComp(gridId).getActivedRow();
                if (Gikam.isEmpty(activedRow)) {
                    Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM', 200);
                    return false
                }
            },
        }
    },

    getAttachmantStruct2: function (gridId, dbTable) {
        let _this = this;
        return {
            type: 'uploader',
            id: _this.getAttachUploaderId(gridId),
            dbTable: dbTable,
            filter: false,
            immediateUpload: true,
            bizId : _this.param.archivesCode,
            genericQuery: false,
            onBeforeUpload: function () {
                if (Gikam.isEmpty(_this.param.archivesCode)) {
                    Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM', 200);
                    return false
                }
            },
        }
    },

    idManager: {
        archivesGrid: 'lims-archives-manage-edit-list-grid',
        archivesBorrowGrid: 'lims-archives-handle-edit-list-grid' + '-borrow',
        archivesMoveGrid: 'lims-archives-handle-edit-list-grid' + '-move',
        archivesDestoryGrid: 'lims-archives-handle-edit-list-grid' + '-destory',
    },

    urlManager: {
        archivesGrid: limsArchivesManage.baseUrl,
        archivesHandleGrid: IFM_CONTEXT + '/secure/basemodule/rm/archives-handles',
    },

    create : function(){
        let _this = this;
        Gikam.create('layout', {
            id : 'lims-person-detail-layout',
            center : {
                items : [ _this.getBtnToolbar(), _this.getBaseInfoTab() ]
            },
            renderTo : workspace.window.$dom
        });
    },

    init :function (param) {
        this.param = param;
        this.create();
    }
};